const app = getApp();
const util = require('../../../utils/util.js');
const lbbService = require('../../../pages/service/service.js');
const lbbTaskService = require('../service/task-service.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userList:[],
        cityShow: '0',
        selectMonth:new Date().getYear()+1900 + '年' +(new Date().getMonth()+1)+'月',
        userName: '全部师傅',
        prov_animationData: {}, 
        user_animationData: {},
        markShow:false,
        minDate:new Date(2010, 0, 1).getTime(),
        currentDate:new Date().getTime(),
        activeKey:0,
        active:0,

        background:"#F4F4F4",
        isBackBtn: true, // 设置是否显示回到顶部按钮
        isEmpty: false, // 设置是否为空数据
        loading: true,
        loaded: false,
        pageNo:1,
        pageSize:10,
    },

    loadmore() {
        let that = this
        that.setData({
            pageNo: that.data.pageNo+1
        })
        that.getHosList()
      },
      refresh() {
        // 下拉到顶部，下拉刷新时执行，请实现清除列表数据，还原分页为初始值的操作
      },
    userToggle: function() {
        this.pickUp()
        if (this.data.cityShow=='4') {
          this.setData({
            cityShow: '0',
          })
        } else {
          this.toggleAnimation(180)
          this.setData({
            cityShow: '4',
          })
        }
        this.setData({
          user_animationData: this.animation.export()
        })
      },
    provToggle: function() {
        this.pickUp()
        if (this.data.cityShow=='1') {
          this.setData({
            cityShow: '0',
          })
        } else {
          this.toggleAnimation(180)
          this.setData({
            cityShow: '1',
            markShow: true
          })
        }
        this.setData({
          prov_animationData: this.animation.export()
        })
    },
    //创建动画
    toggleAnimation: function(angle) {
      var animation = wx.createAnimation({
        timingFunction: "ease"
      })
      this.animation = animation;
      animation.rotate(angle).step()
    },
    setUser:function (e) {
        console.log("setUser-->",e)
       
        let userIndex = e.target.dataset.index; //当前点击的索引
        let userName = e.target.dataset.name;
        let userId = e.target.dataset.id;
        this.toggleAnimation(0)
        this.setData({
            cityShow: '0',
            userIndex,
            userId,
            userName,
            user_animationData: this.animation.export()
        })
        this.getHosList()
    },
    cancelSelect: function(){
        this.pickUp()
        this.setData({
          cityShow: '0'
        })
    },
    pickUp: function(){
        if (this.data.cityShow == '1') {
          this.toggleAnimation(0)
          this.setData({
            prov_animationData: this.animation.export()
          })
        } else if (this.data.cityShow == '2') {
          this.toggleAnimation(0)
          this.setData({
            city_animationData: this.animation.export()
          })
        } else if (this.data.cityShow == '3') {
          this.toggleAnimation(0)
          this.setData({
            region_animationData: this.animation.export()
          })
        } else if (this.data.cityShow == '4') {
          this.toggleAnimation(0)
          this.setData({
            user_animationData: this.animation.export()
          })
        }
    },
    formatDate(date) {
        date = new Date(date);
        return `${date.getYear() +1900 }年${date.getMonth() + 1}月`;
    },
    onMarkClose() {
        this.toggleAnimation(0)
        this.setData({ markShow: false,cityShow:0,prov_animationData: this.animation.export() });
    },
    onMarkConfirm(event) {
        this.setData({
          markShow: false,
          selectMonth:this.formatDate(event.detail)
        });
        this.toggleAnimation(0)
        this.setData({
            cityShow: '0',
            prov_animationData: this.animation.export()
        })
        this.getHosList()
    },
    tabChange(e){
        let that = this
        that.data.pageNo =1
        this.setData({
            active:e.detail.index,
        })
        that.getHosList()
    },
    // 巡检截图列表
    getHosList(){
        let that = this
        let parmas = {
            pageNo:that.data.pageNo,
            pageSize:that.data.pageSize,
            isScreenshot:that.data.active,
            month:that.data.selectMonth,
            userId:that.data.userId
        }
        wx.showLoading({
          title: '加载中',
        })
        lbbTaskService.getInspectUnshotList(parmas,function(res){
            if(that.data.pageNo==1) {
                let lists = [ ...res.data.records]
                if(lists.length==0) {
                    that.setData({
                        loading: false,
                        loaded: true,
                        TotalPage:res.data.pages,
                        hospitalList:lists
                    })
                    wx.hideLoading()
                } else {
                    that.setData({
                        loading: false,
                        loaded: true,
                        TotalPage:res.data.pages,
                        hospitalList:lists
                    })
                    wx.hideLoading()
                }
            } else {
                let lists = [...that.data.hospitalList, ...res.data.records]
                console.log(lists)
                if(lists.length==0) {
                    that.setData({
                        loading: false,
                        loaded: true,
                        TotalPage:res.data.pages,
                        hospitalList:lists
                    })
                    wx.hideLoading()
                } else {
                    that.setData({
                        loading: false,
                        loaded: true,
                        TotalPage:res.data.pages,
                        hospitalList:lists
                    })
                    wx.hideLoading()
                }
            }

        })
    }, 
    getUserList() {
        let that = this
        lbbTaskService.getDeskUserList({deptTaskType:0},function(res){
            if (res.data.length == 1) {
                that.setData({
                    userId: res.data[0].id,
                    userName: res.data[0].name
                })
            } else {
                wx.getUserInfo({
                    success(res) {
                        that.setData({
                            userName: res.userInfo.nickName,
                        })
                    }
                })
            }
            that.setData({
                userList:res.data
            })
        })
    }, 
    getUserIdByName(name) {
        let userList = this.data.userList
        if(userList.length>0){
            for(let i=0;i<userList.length;i++) {
                if(userList[i].name==name) {
                    return userList[i].id
                }
            }
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getUserList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})